package model;

import java.io.Serializable;
import java.util.EnumMap;
import java.util.EnumSet;

/**
 * The User class is responsible for storing all user data. It also contains
 * enums for user types as well as defines editing rights for specific user
 * groups.
 * <p>
 * @author Kevin, Tim
 * @version 2014-05-17
 */
public class User implements Serializable {

    private static final EnumMap<User.Type, EnumSet> editingRights = setEditingRights();
    private int id;
    private String username, password, firstName, lastName;
    private Type userType;
    private Department department;

    /**
     * Constructor for class User
     *
     * @param id User id
     * @param username User's username
     * @param password User's password
     * @param firstName User's first name
     * @param lastName User's last name
     * @param userType User type
     * @param department User's department
     */
    public User(int id, String username, String password, String firstName, String lastName, Type userType, Department department) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.userType = userType;
        this.department = department;
    }

    // <editor-fold defaultstate="collapsed" desc=" GET&SET METHODS ">

    /**
     * Gets user id
     * @return user id
     */
        public int getId() {
        return id;
    }

    /**
     * Sets user id
     * @param Id
     */
    public void setId(int Id) {
        this.id = Id;
    }

    /**
     * Gets user's username
     * @return username
     */
    public String getUsername() {
        return username;
    }

    /**
     * Sets user's username
     * @param username
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * Gets user's password
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     * Sets users password
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Gets user's first name
     * @return user's first name
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Sets user's first name
     * @param firstName
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * Gets user's last name
     * @return user's last name
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Sets user's last name
     * @param lastName
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * Gets user type
     * @return user type
     */
    public Type getUserType() {
        return userType;
    }

    /**
     * Sets user type
     * @param userType
     */
    public void setUserType(Type userType) {
        this.userType = userType;
    }

    /**
     * Gets user's department
     * @return department
     */
    public Department getDepartment() {
        return department;
    }
    
    /**
     * Sets user's department
     * @param department
     */
    public void setDepartment(Department department) {
        this.department = department;
    }
    // </editor-fold>

    @Override
    public String toString() {
        return lastName + ", " + firstName;
    }

    /**
     * Enum containing all user types.
     */
    public enum Type implements Serializable {

        COORDINATOR("Miljösamordnare"), MANAGER("Enhetschef"), ADMINISTRATOR("Handläggare");

        private final String name;

        private Type(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return name;
        }
    }

    /**
     * Creates editing rights for user-types.
     * <p>
     * @return EnumMap<User.Type, EnumSet> editing rights
     */
    private static EnumMap<User.Type, EnumSet> setEditingRights() {
        EnumMap<User.Type, EnumSet> editingRights = new EnumMap<>(User.Type.class);

        // Editing rights for Coordinators
        EnumSet<Cases.Field> coordinatorRights = EnumSet.allOf(Cases.Field.class);
        coordinatorRights.remove(Cases.Field.CASE_ID);
        coordinatorRights.remove(Cases.Field.RESPONSIBLE_ADMINISTRATOR);
        editingRights.put(Type.COORDINATOR, coordinatorRights);

        // Editing rights for Managers
        editingRights.put(Type.MANAGER, EnumSet.of(Cases.Field.RESPONSIBLE_ADMINISTRATOR));

        // Editing rights for Administrators
        EnumSet<Cases.Field> administratorRights = EnumSet.of(Cases.Field.STATUS);
        editingRights.put(Type.ADMINISTRATOR, administratorRights);

        return editingRights;
    }

    /**
     * Returns a map containing editing rights with user-types as keys and data
     * fields as values. If the map contains a field under a specific user-type
     * as key, it means that that user group has the right to edit that field.
     * <p>
     * @return EnumMap<Type, EnumSet> editing rights
     */
    public static EnumMap<Type, EnumSet> getEditingRights() {
        return editingRights;
    }

}
